﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Beaker : BaseBrokenObj
{
    public Animator m_WaterAnimator;
    public GameObject m_Water;
    public static int m_FtNum;//酚酞滴数

    public WaterPoint[] m_WaterPoint;

    public BeakerBrush m_BeakerBrush;

    private void Start()
    {
        base.Start();
        lastPos = transform.position; //设置lastPosition
    }

    public override void OnHandleTrigger(Transform camera, NoloDeviceType handleType)
    {
        base.OnHandleTrigger(camera, handleType);
        m_WaterAnimator.SetBool("take", true);
    }

    public override void OnReleseTrigger(NoloDeviceType handleType)
    {
        base.OnReleseTrigger(handleType);
        m_WaterAnimator.SetBool("take", false);
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.name == "Rubber_stopper_double_Ko")
        {
            if (StepsManager.Instance.StepThreeNum[2])
            {
                StepsManager.Instance.StepFourNum[0] = true;
                StepsManager.Instance.SetStepFour(0);
            }
        }
    }

    bool m_CanClear;
    private void Update()
    {
        float z = transform.eulerAngles.z;
        if (z > 90 && z < 270)
        {
            if (isCatch && StepsManager.Instance.StepFourNum[1])
            {
                m_CanClear = true;
                m_Water.SetActive(false);
            }
        }

        if (RoundBottpmFlask.Instance.m_IsStart && m_Water.transform.localScale.y > 2.8f)
        {
            m_Water.transform.localScale -= new Vector3(0, 0.03f, 0);
        }

        if (z > 20 && z < 340)
        {
            m_WaterAnimator.SetBool("take", false);
            m_WaterAnimator.enabled = false;
            for (int i = 0; i < m_WaterPoint.Length; i++)
            {
                m_WaterPoint[i].ResetPosition(z);
            }
        }
        else
        {
            m_WaterAnimator.enabled = true;
            if (isCatch)
            {
                m_WaterAnimator.SetBool("take", true);
            }
            else
            {
                m_WaterAnimator.SetBool("take", false);
            }
        }

        if (m_BeakerBrush.isCatch && isCatch && m_CanClear)
        {
            if ((m_BeakerBrush.transform.position.x < transform.position.x
            && (m_BeakerBrush.transform.position - transform.position).magnitude < 0.1f)
            || (m_BeakerBrush.transform.position.x > transform.position.x
            && (m_BeakerBrush.transform.position - transform.position).magnitude < 0.3f))
            {
                transform.eulerAngles = new Vector3(0, 0, -90);
                m_BeakerBrush.SetScale(transform.position);
            }
        }

    }

    Vector3 towardPos;
    Vector3 lastPos;
    float speed;

    private void LateUpdate()
    {
        if (RoundBottpmFlask.Instance.m_IsStart && !StepsManager.Instance.StepThreeNum[2])
            canCatsh = false;
        else
            canCatsh = true;

        towardPos = transform.position - lastPos; //两帧间向量差  
        speed = towardPos.magnitude / Time.deltaTime; // 距离/时间 
        lastPos = transform.position; //设置lastPosition
        m_WaterAnimator.SetFloat("speed", speed);
    }

    public void SetStartMove()
    {
        m_rigidbody.useGravity = false;
        m_rigidbody.isKinematic = true;
        m_collider.isTrigger = true;
    }

    public void SetStopMove()
    {
        m_rigidbody.useGravity = true;
        m_rigidbody.isKinematic = false;
        m_collider.isTrigger = false;
    }


    //记录每个大步骤的初始状态
    #region
    public void SetBigStepTwo()
    {
        ResetPosition();
        m_Water.SetActive(true);
        m_Water.transform.localScale = new Vector3(5, 9.5f, 5);
    }

    public void SetBigStepThree()
    {
        transform.localPosition = new Vector3(-0.115f, 0.06f, 0f);
        m_Water.SetActive(true);
        m_Water.transform.localScale = new Vector3(5, 9.5f, 5);
    }

    public void SetBigStepFour()
    {
        m_ClearNum = 0;
        transform.localPosition = new Vector3(-0.115f, 0.06f, 0f);
        m_Water.SetActive(true);
        m_Water.transform.localScale = new Vector3(5, 2.8f, 5);

    }

    public void SetBigStepFive()
    {
        ResetPosition();
        m_Water.SetActive(true);
        m_Water.transform.localScale = new Vector3(5, 2.8f, 5);
    }
    #endregion

    int m_ClearNum;
    private void OnTriggerEnter(Collider other)
    {
        if (other.name == "BeakerBrush")
        {
            if (m_CanClear)
            {
                m_ClearNum++;
                if (m_ClearNum >= 2)
                {
                    StepsManager.Instance.StepFiveNum[1] = true;
                    StepsManager.Instance.SetStepFive(1);
                }
            }
        }
    }
}
